(function (angular) {
  'user strict';
  angular
    .module('fs.filter', [])
    .filter('encode', ['$filter', 'Constants', function ($filter, Constants) {
      return function (value) {
        if (!value || angular.isNumber(value) || !angular.isString(value)) {
          return value;
        } else if ('*' == value) {
          return $filter('all')(value);
        }
        if (value.indexOf(Constants.ENCODE_SEPARATOR) != -1) {
          value = value.substring(0, value.indexOf(Constants.ENCODE_SEPARATOR));
        }
        return value;
      };
    }])
    .filter('trustedUrl', ['$sce', function ($sce) {
      return function (value) {
        if (!value) {
          return value;
        }
        return $sce.trustAsUrl(value);
      };
    }])
    .filter('trustedResourceUrl', ['$sce', function ($sce) {
      return function (value) {
        if (!value) {
          return value;
        }
        return $sce.trustAsResourceUrl(value);
      };
    }])
    .filter('trustedHtml', ['$sce', function ($sce) {
      return function (value) {
        if (!value) {
          return value;
        }
        return $sce.trustAsHtml(value);
      };
    }])
    .filter('numberFormat', ['$filter', '$locale', 'formatService', function ($filter, $locale, formatService) {
      return function (number, pattern) {
        if (isNaN(number)) {
          return number;
        }
        return formatService.formatNumber(number, pattern);
      };
    }])
    .filter('moneyFormat', ['$filter', '$locale', 'formatService', function ($filter, $locale, formatService) {
      return function (money, pattern) {
        if (isNaN(money)) {
          return money;
        }
        return formatService.formatMoney(money, pattern);
      };
    }])
    .filter('moneyFormatWithCcy', ['$filter', '$locale', 'formatService', function ($filter, $locale, formatService) {
      return function (money, ccy) {
        if (isNaN(money)) {
          return money;
        }
        money = formatService.formatMoney(money, ccy);
        return '<div class="report-amount"><h5>' + ccy + '</h5><h5>' + money + '</h5></div>'
      };
    }])
    .filter('statusCodeFilter', ['$filter', '$locale', 'formatService', function ($filter, $locale, formatService) {
      return function (statusCode) {
        statusCode = angular.lowercase(statusCode);
        return '<div style="text-align: center;"><span class="icon icon-status-' + statusCode + '"></span></div>'
      };
    }]).
    filter('percentFormat', ['$filter', '$locale', 'formatService', function ($filter, $locale, formatService) {
      return function (number, pattern) {
        return formatService.formatPercentage(number, pattern);
      };
    }])
    .filter('dateFormat', ['$filter', '$locale', 'formatService', function ($filter, $locale, formatService) {
      return function (date, pattern) {
        if (angular.isString(date)) {
          date = formatService.parseDate(date, 'longTimestamp');
        }
        return $filter('date')(date, pattern);
      };
    }])
    .filter('removeSiteCode', ['$filter', '$locale', 'formatService', function ($filter, $locale, formatService) {
      return function (data, pattern) {
        //in the list page if db product Code like 'FSCSBK:PROD_CODE1', the Product code in the list only show 'PROD_CODE1'. the siteCode length is 6.
        if (data) return data.substring(7, data.length);
      };
    }])
    .filter('status', ['$filter', '$locale', 'SYS_PARAMS', '$translate', function ($filter, $locale, SYS_PARAMS, $translate) {
      return function (dataStatus, pattern) {
        var label = dataStatus ? (SYS_PARAMS.SETUP_STATUS[dataStatus.toUpperCase()] || dataStatus) : dataStatus;
        return $filter('translate')(label);
      };
    }])
    .filter('i18n', ['$filter', '$locale', '$translate', function ($filter, $locale, $translate) {
      return function (resKey, pattern) {
        return $filter('translate')(resKey);
      };
    }])
    .filter('loanStatus', ['$filter', '$locale', 'SYS_PARAMS', '$translate', function ($filter, $locale, SYS_PARAMS, $translate) {
      return function (loanStatus, pattern) {
        var label = loanStatus ? (SYS_PARAMS.LOAN_STATUS[loanStatus.toUpperCase()] || loanStatus) : loanStatus;
        return $filter('translate')(label);
      };
    }])
    .filter('pendingStatus', ['$filter', '$locale', 'SYS_PARAMS', '$translate', function ($filter, $locale, SYS_PARAMS, $translate) {
      return function (pendingStatus, pattern) {
        var label = pendingStatus ? (SYS_PARAMS.PENDING_STATUS[pendingStatus.toUpperCase()] || pendingStatus) : pendingStatus;
        return $filter('translate')(label);
      };
    }])
    .filter('userStatus', ['$filter', '$locale', 'SYS_PARAMS', '$translate', function ($filter, $locale, SYS_PARAMS, $translate) {
      return function (userStatus, pattern) {
        var label = userStatus ? (SYS_PARAMS.USER_STATUS[userStatus.toUpperCase()] || userStatus) : userStatus;
        return $filter('translate')(label);
      };
    }])
    .filter('localeStatus', ['$filter', '$locale', 'SYS_PARAMS', '$translate', function ($filter, $locale, SYS_PARAMS, $translate) {
      return function (localeStatus, pattern) {
        var label = localeStatus ? (SYS_PARAMS.LOCALE_STATUS[localeStatus.toUpperCase()] || localeStatus) : localeStatus;
        return $filter('translate')(label);
      };
    }])
    .filter('reqStatus', ['$filter', '$locale', 'SYS_PARAMS', '$translate', function ($filter, $locale, SYS_PARAMS, $translate) {
      return function (reqStatus, pattern) {
        var label = reqStatus ? (SYS_PARAMS.REQ_STATUS[reqStatus.toUpperCase()] || reqStatus) : reqStatus;
        return $filter('translate')(label);
      };
    }])
    .filter('resStatus', ['$filter', '$locale', 'SYS_PARAMS', '$translate', function ($filter, $locale, SYS_PARAMS, $translate) {
      return function (resStatus, pattern) {
        var label = resStatus ? (SYS_PARAMS.RES_STATUS[resStatus.toUpperCase()] || resStatus) : resStatus;
        return $filter('translate')(label);
      };
    }])
    .filter('responseStatus', ['$filter', '$locale', 'SYS_PARAMS', '$translate', function ($filter, $locale, SYS_PARAMS, $translate) {
      return function (responseStatus, pattern) {
        var label = responseStatus ? (SYS_PARAMS.RESPONSE_STATUS[responseStatus.toUpperCase()] || responseStatus) : responseStatus;
        return $filter('translate')(label);
      };
    }])
    .filter('yesNo', ['$filter', '$locale', 'SYS_PARAMS', '$translate', function ($filter, $locale, SYS_PARAMS, $translate) {
      return function (flag, pattern) {
        var label = flag ? (SYS_PARAMS.YES_NO[flag.toUpperCase()] || flag) : flag;
        return $filter('translate')(label);
      };
    }])
    .filter('companystatus', ['$filter', '$locale', 'SYS_PARAMS', '$translate', function ($filter, $locale, SYS_PARAMS, $translate) {
      return function (flag, pattern) {
        var label = flag ? (SYS_PARAMS.COMPANY_STATUS[flag.toUpperCase()] || flag) : flag;
        return $filter('translate')(label);
      };
    }])
    .filter('crDr', ['$filter', '$locale', 'SYS_PARAMS', '$translate', function ($filter, $locale, SYS_PARAMS, $translate) {
      return function (flag, pattern) {
        var label = flag ? (SYS_PARAMS.CR_DR[flag.toUpperCase()] || flag) : flag;
        return $filter('translate')(label);
      };
    }])
    .filter('payType', ['$filter', '$locale', 'SYS_PARAMS', '$translate', function ($filter, $locale, SYS_PARAMS, $translate) {
      return function (flag, pattern) {
        var label = flag ? (SYS_PARAMS.PAY_TYPE[flag.toUpperCase()] || flag) : flag;
        return $filter('translate')(label);
      };
    }])
    .filter('intCollMethodType', ['$filter', '$locale', 'SYS_PARAMS', '$translate', function ($filter, $locale, SYS_PARAMS, $translate) {
      return function (flag, pattern) {
        var label = flag ? (SYS_PARAMS.INT_COLL_METHOD[flag.toUpperCase()] || flag) : flag;
        return $filter('translate')(label);
      };
    }])
    .filter('payMdbFileStatus', ['$filter', '$locale', 'SYS_PARAMS', '$translate', function ($filter, $locale, SYS_PARAMS, $translate) {
      return function (flag, pattern) {
        var label = flag ? (SYS_PARAMS.PAYMDB_FILE_STATUS[flag.toUpperCase()] || flag) : flag;
        return $filter('translate')(label);
      };
    }])
    .filter('swiftStatus', ['$filter', '$locale', 'SYS_PARAMS', '$translate', function ($filter, $locale, SYS_PARAMS, $translate) {
      return function (flag, pattern) {
        var label = flag ? (SYS_PARAMS.SWIFT_STATUS[flag.toUpperCase()] || flag) : flag;
        return $filter('translate')(label);
      };
    }])
    .filter('paymdbStatus', ['$filter', '$locale', 'SYS_PARAMS', '$translate', function ($filter, $locale, SYS_PARAMS, $translate) {
      return function (flag, pattern) {
        var label = flag ? (SYS_PARAMS.PAYMDB_STATUS[flag.toUpperCase()] || flag) : flag;
        return $filter('translate')(label);
      };
    }])
    .filter('all', ['$filter', '$locale', 'SYS_PARAMS', '$translate', function ($filter, $locale, SYS_PARAMS, $translate) {
      return function (flag, pattern) {
        var label = flag ? (SYS_PARAMS.ALL[flag.toUpperCase()] || flag) : flag;
        return $filter('translate')(label);
      };
    }])
    .filter('parmValueUitype', ['$filter', '$locale', 'SYS_PARAMS', '$translate', function ($filter, $locale, SYS_PARAMS, $translate) {
      return function (flag, pattern) {
        var label = flag ? (SYS_PARAMS.PARM_VALUE_UITYPE[flag.toUpperCase()] || flag) : flag;
        return $filter('translate')(label);
      };
    }])
    .filter('dynaparmType', ['$filter', '$locale', 'SYS_PARAMS', '$translate', function ($filter, $locale, SYS_PARAMS, $translate) {
      return function (flag, pattern) {
        var label = flag ? (SYS_PARAMS.SETUP_DYNAPARM_LIST_DATATYPE[flag.toUpperCase()] || flag) : flag;
        return $filter('translate')(label);
      };
    }])
    .filter('reportMode', ['$filter', '$locale', 'SYS_PARAMS', '$translate', function ($filter, $locale, SYS_PARAMS, $translate) {
      return function (flag, pattern) {
        var label = flag ? (SYS_PARAMS.REPORT_MODE[flag.toUpperCase()] || flag) : flag;
        return $filter('translate')(label);
      };
    }])
    .filter('json', ['$filter', function ($filter) {
      return function (value) {
        if (value) {
          var result = "";
          var obj = angular.fromJson(value);
          var label = $filter('translate')(obj["label"]);
          for (var key in obj) {
            if (key != "label") {
              result += ($filter('translate')(label + key.toLowerCase()) + "=" + obj[key] + ";");
            }
          }
          return result.substring(0, result.length - 1);
        }
        return value;
      };
    }])
    .filter("sensitive", function () {
      return function (value, positiveOffset, negativeOffset, mask) {
        if (value == undefined) {
          value = "";
        }
        return encryption(value, positiveOffset, negativeOffset, mask, '*');
      };
      function encryption(value, positiveOffset, negativeOffset, mask, symbol) {
        var result;
        function getAsterisk(a1) {
          var asterisk = "";
          for (var i = 0; i < a1; i++) {
            asterisk += symbol;
          }
          return asterisk;
        }
        if (positiveOffset < 0) {
          positiveOffset = 0;
        }
        if (negativeOffset < 0) {
          negativeOffset = 0;
        }
        if (mask) {
          if (positiveOffset + negativeOffset < value.length) {
            var headStr = value.substring(0, positiveOffset);
            var endStr = value.substring(value.length - negativeOffset);
            result = headStr + getAsterisk(value.length - positiveOffset - negativeOffset) + endStr;
          } else {
            result = value;
          }
          return result;
        } else {
          if (positiveOffset + negativeOffset < value.length) {
            var str = value.substring(positiveOffset, value.length - negativeOffset);
            result = getAsterisk(positiveOffset) + str + getAsterisk(negativeOffset);
          } else {
            result = getAsterisk(value.length);
          }
          return result;
        }
      }
    })
    .filter("zeroEmpty", function () {
      return function (value) {
        if (value === 0) {
          value = "";
        }
        return value;
      };
    })
    .filter("percentageFilter", function () {
      return function (value) {
        return value + "%";
      };
    })
    .filter("options", ['$filter', '$locale', '$translate', function ($filter, $locale, $translate) {
      return function (optionValue, options) {
        var optionLabel = '';
        if (options && angular.isArray(options)) {
          for (var i = 0; i < options.length; i++) {
            if ($filter('encode')(optionValue) == $filter('encode')(options[i].value)) {
              optionLabel = $filter('translate')(options[i].label);
              break;
            }
          }
        }
        return optionLabel;
      };
    }]);
})(angular);
